package com.yanqu.road.server.cmd.strongholdwar.eliminate;

import com.yanqu.road.entity.activity.stronghold.CrossStrongholdEliminateDefendPatrons;
import com.yanqu.road.entity.activity.stronghold.CrossStrongholdEliminateUnion;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.StrongholdWarProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.strongholdwar.AttackUnionResult;
import com.yanqu.road.server.manager.strongholdwar.CrossStrongholdActivityMgr;
import com.yanqu.road.server.manager.strongholdwar.CrossStrongholdMgr;
import com.yanqu.road.server.manager.strongholdwar.CrossStrongholdRankMgr;
import com.yanqu.road.server.pb.CrossStrongholdPb;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;

import java.math.BigInteger;

@Cmd(code = Protocol.C_CROSS_STRONGHOLD_CHALLENGE_UNION, desc = "挑战联盟")
public class CrossChallengeUnionCmd extends CrossUnionCommand {
    private int activityId;
    private int id;
    private long userId;
    private int patronsId;
    private BigInteger damage;
    private UserBaseInfo userBaseInfo;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        StrongholdWarProto.CrossAttackEliminatePatronsReqMsg req = StrongholdWarProto.CrossAttackEliminatePatronsReqMsg.parseFrom(packet.getBytes());
        activityId = req.getActivityId();
        id = req.getId();
        userId = packet.getPlayerId();
        patronsId = req.getPatronsId();
        damage = new BigInteger(req.getDamage());
        userBaseInfo = PlayerBasePb.parseToUserBaseInfo(req.getUserInfo());
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        AttackUnionResult result = CrossStrongholdMgr.attackUnion(activityId, userId, userBaseInfo, patronsId, damage, id);
        StrongholdWarProto.CrossAttackEliminatePatronsRespMsg.Builder resp = StrongholdWarProto.CrossAttackEliminatePatronsRespMsg.newBuilder();
        ret = result.getRet();
        resp.setRet(ret);
        if (ret == 0) {
            CrossStrongholdEliminateDefendPatrons patrons = result.getPatrons();
            if (patrons != null) {
                resp.setPatrons(CrossStrongholdPb.parseEliminateChallengePatronsMsg(patrons));
            }
            resp.setDamage(result.getDamage());
            resp.setKill(result.getKillNum() > 0);
        }
        CrossStrongholdEliminateUnion union = CrossStrongholdMgr.chooseChallengeUnion(activityId, groupId, id);
        int rank = CrossStrongholdRankMgr.getCrossActivityUnionRank(activityId, CrossStrongholdActivityMgr.CONDITION_TYPE_ELIMINATE_UNION, union.getServerId(), union.getUnionUid());
        resp.setUnionInfo(CrossStrongholdPb.parseEliminateUnionMsg(union, rank));
        resp.setChallengePatronsId(patronsId);
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(GameProtocol.S_CROSS_STRONGHOLD_CHALLENGE_UNION, resp);
        MessageHelper.sendPacket(serverId, userId, pbMsg);
    }

    @Override
    public void sendData() {

    }
}
